package com.cqupt.sendMes.controller;

import com.cqupt.sendMes.dto.TaskDTO;
import com.cqupt.sendMes.dto.TaskQueryDTO;
import com.cqupt.sendMes.entity.Task;
import com.cqupt.sendMes.enums.HttpStatusEnum;
import com.cqupt.sendMes.enums.TaskActivateEnum;
import com.cqupt.sendMes.enums.TaskQueryType;
import com.cqupt.sendMes.enums.TaskTypeEnum;
import com.cqupt.sendMes.exception.BusinessException;
import com.cqupt.sendMes.service.TaskService;
import com.cqupt.sendMes.utils.Result;
import com.cqupt.sendMes.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Objects;

@Slf4j
@RestController
@Api(tags = "任务关联")
@RequestMapping("/task")
public class TaskController {
    @Autowired
    private TaskService taskService;

    @PostMapping("/add")
    @ApiOperation("添加任务")
    public Result<Boolean> addTask(@RequestBody @Validated TaskDTO taskDTO){
        TaskTypeEnum taskTypeEnum = Arrays.stream(TaskTypeEnum.values())
                .filter(s -> s.getCode() == (taskDTO.getTaskType()))
                .findFirst()
                .orElse(null);
        if(taskTypeEnum == null){
            log.warn("任务类型参数错误!");
            throw new BusinessException(HttpStatusEnum.ILLEGAL_REQUEST_EXP.getCode(), HttpStatusEnum.ILLEGAL_REQUEST_EXP.getMessage());
        }
        log.info("准备执行添加任务，任务名：{}", taskDTO.getTaskName());
        return Result.createSuccessResult(taskService.addTask(taskDTO));
    }

    @GetMapping("/delete")
    @ApiOperation("删除任务")
    public Result<Boolean> deleteTask(@RequestParam("taskId") @NonNull Integer taskId){
        log.info("准备执行删除任务，任务ID：{}", taskId);
        return Result.createSuccessResult(taskService.deleteTask(taskId));
    }

    @PostMapping("/update")
    @ApiOperation("更新任务")
    public Result<Boolean> updateTask(@RequestBody @Validated TaskDTO taskDTO){
        log.info("准备执行更新任务，任务ID：{}", taskDTO.getId());
        return Result.createSuccessResult(taskService.updateTask(taskDTO));
    }

    @PostMapping("/list")
    @ApiOperation("查询任务列表")
    public Result<PageResult<Task>> queryTask(@RequestBody @Validated TaskQueryDTO taskQueryDTO){
        log.info("查询任务类型：{}", taskQueryDTO.getTaskType());
        return Result.createSuccessResult(taskService.queryTask(taskQueryDTO));
    }

    @GetMapping("/change")
    @ApiOperation("修改任务状态")
    public Result<Boolean> changeStatus(@RequestParam("taskId") @NonNull Integer taskId, @RequestParam("status") @NonNull String status){
        TaskActivateEnum taskActivateEnum = Arrays.stream(TaskActivateEnum.values())
                .filter(s -> Objects.equals(s.getCode(), status))
                .findFirst()
                .orElse(null);
        if(taskActivateEnum == null){
            log.warn("任务激活状态参数错误!");
            throw new BusinessException(HttpStatusEnum.ILLEGAL_REQUEST_EXP.getCode(), HttpStatusEnum.ILLEGAL_REQUEST_EXP.getMessage());
        }
        log.info("准修改任务状态，任务ID：{}，状态：{}", taskId, status);
        //执行
        return Result.createSuccessResult(taskService.changeStatus(taskId, status));

    }

    @GetMapping("bind")
    @ApiOperation("绑定任务")
    public Result<Boolean> bandTask (@RequestParam("taskId") @NonNull Integer taskId){
        log.info("准备执行绑定任务，任务ID：{}", taskId);
        return Result.createSuccessResult(taskService.bandTask(taskId));
    }

    @GetMapping("/unbind")
    @ApiOperation("取消绑定")
    public Result<Boolean> unBandTask (@RequestParam("taskId") @NonNull Integer taskId){
        log.info("准备执行取消绑定任务，任务ID：{}", taskId);
        return Result.createSuccessResult(taskService.unBandTask(taskId));
    }
}
